﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RDPProxyClient
{
    public partial class Form1 : Form
    {
        private readonly List<ClientSetting> _clientSettings = new List<ClientSetting>();



        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            //DoConnect();
            _clientSettings.Add(new ClientSetting { ServerName = "rcasd", ServerAddress = "rcasd.iplink.com.cn", ServerPort = 8080, ClientPort = 3389 });
            _clientSettings.Add(new ClientSetting { ServerName = "2", ServerAddress = "2", ServerPort = 2 });
            _clientSettings.Add(new ClientSetting { ServerName = "3", ServerAddress = "3", ServerPort = 3 });

            serverInfoBindingSource.DataSource = _clientSettings;
            cmbServer.SelectedIndex = -1;
            cmbServer.SelectedIndex = 0;
        }


        private void CmbServer_TextChanged(object sender, EventArgs e)
        {
            var server = _clientSettings.SingleOrDefault(t => t.ServerName == cmbServer.Text.ToString());
            //            if (server == null)
            //            {
            //                return;
            //            }
            txtServerAddress.Text = server?.ServerAddress;
            txtServerPort.Value = server?.ServerPort ?? 8080;
            txtClientPort.Value = server?.ClientPort ?? 3389;
        }

        private CoreCode cc = null;
        private Thread _thread;

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (cc == null)
            {
                cc = new CoreCode(new ClientSetting
                {
                    ServerName = cmbServer.Text,
                    ServerAddress = txtServerAddress.Text,
                    ServerPort = Convert.ToInt32(txtServerPort.Value),
                    ClientPort = Convert.ToInt32(txtClientPort.Value)
                });
            }

            cc.ClientSetting = new ClientSetting
            {
                ServerName = cmbServer.Text,
                ServerAddress = txtServerAddress.Text,
                ServerPort = Convert.ToInt32(txtServerPort.Value),
                ClientPort = Convert.ToInt32(txtClientPort.Value)
            };
            //ackgroundWorker1.RunWorkerAsync();
            _thread = new Thread(cc.DoConnect);
            _thread.Start();
        }


        private void btnStop_Click(object sender, EventArgs e)
        {
            cc.Stop();
            cc.Dispose();
            _thread.Abort();
            //_thread.Join();
            //backgroundWorker1.CancelAsync();
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                }
                cc.DoConnect();
            }



        }
    }
}
